﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DrSoftsFrame.Servers;

namespace PreeRegisterDAL
{
    public class CancelOrderDAL
    {
        public static DataSet CancelOrder(string iWorkPlanID, string iRegisteNum, string OrderId, string iDetailId, bool bPreTime)
        {
            //if (!PreeRegisterDAL.LHL.AvailableCountDAL.bDataBase)
            //{
            //    return AvailableCountDAL.GetData("CancelOrder.xml");
            //}
            ClinicDb mydb = new ClinicDb();
            try
            {
                SqlCommand cmd = new SqlCommand();//去掉bLock=1的条件和bRegisteFlag=0的条件  lhl20140327
                string sqlStr = @"select iDetailId,bLock,bcheck from opdworkplandetail where (iWorkPlanID=@iWorkPlanID and iNum=@iRegisteNum and iDetailId=@iDetailId and isnull(bLock,0)=1);";
                if (bPreTime)
                {
                    sqlStr += @"select iRegisterPreeNo,bRegisteFlag,iUpOpID,dUpdate,nReason from OPDRegisterPree where iWorkPlanID=@iWorkPlanID and iRegisteNum=@iRegisteNum and cPreeCode=@OrderId and iWorkPlanDetailId=@iDetailId;";  //取消预约时查询OPDRegisterPree时去掉iDetailId条件去掉，解决号源过期问题 lhl20131214//取消预约时查询OPDRegisterPree时添加iDetailId条件，解决号源过期问题 lhl20141203
                }
                else
                {
                    sqlStr += @"select iRegisterPreeNo,bRegisteFlag,iUpOpID,dUpdate,nReason from OPDRegisterPree where iWorkPlanID=@iWorkPlanID and iRegisteNum=@iRegisteNum and cPreeCode=@OrderId;"; 
                }
                sqlStr += @"select iWorkPlanID,isnull(npreengageNum,0) npreengageNum,isnull(nMaxPreengageNum,0) nMaxPreengageNum from OPDWorkPlan where iWorkPlanID=@iWorkPlanID;";
                cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@iWorkPlanID", System.Data.SqlDbType.Int));
                cmd.Parameters["@iWorkPlanID"].Value = iWorkPlanID;
                cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@iDetailId", System.Data.SqlDbType.Int));
                if (bPreTime)
                {
                    cmd.Parameters["@iDetailId"].Value = iDetailId;
                }
                else
                {
                    cmd.Parameters["@iDetailId"].Value = -1000;
                }
                cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@iRegisteNum", System.Data.SqlDbType.Int));
                cmd.Parameters["@iRegisteNum"].Value = iRegisteNum;
                cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@OrderId", System.Data.SqlDbType.VarChar,20));
                cmd.Parameters["@OrderId"].Value = OrderId;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = sqlStr;
                DataSet ds = mydb.ExecuteSqlCmdFillDs(cmd);
                //AvailableCountDAL.SaveXML("CancelOrder.xml", ds.GetXml());
                return ds;
            }
            catch (Exception err)
            {
                throw err;
            }
            finally
            {
                mydb.Dispose();
            }
        }

        /// <summary>
        /// 根据订单号查询预约信息  liujia151208
        /// </summary>
        /// <param name="ordercode"></param>
        /// <returns></returns>
        public static DataSet CancelOrder(string ordercode)
        {

            ClinicDb mydb = new ClinicDb();
            try
            {
                string sqlstr = string.Format(@"--查询预约表
                                                select * into #OPDRegisterPree
                                                from OPDRegisterPree 
                                                where orderCode='{0}'

                                                --查询排班明细表
                                                select a.iDetailId,a.bLock,a.bCheck 
                                                from opdworkplandetail a
                                                join #OPDRegisterPree b on a.iWorkPlanID=b.iWorkPlanId and a.iNum=b.iRegisteNum
                                                where isnull(bLock,0)=1

                                                --返回预约表
                                                select iRegisterPreeNo,bRegisteFlag,iUpOpID,dUpdate,nReason,bNowDayFlag 
                                                from #OPDRegisterPree 

                                                --查询排班主表
                                                select a.iWorkPlanID,isnull(a.nPreengageNum,0) npreengageNum,isnull(a.nMaxPreengageNum,0) nMaxPreengageNum 
                                                from OPDWorkPlan a
                                                join #OPDRegisterPree b on a.iWorkPlanID=b.iWorkPlanId", ordercode);
                DataSet ds = mydb.ExecuteSelect(sqlstr);
                return ds;
            }
            catch (Exception err)
            {
                throw err;
            }
            finally
            {
                mydb.Dispose();
            }
        }

        /// <summary>
        /// 同煤版取消预约  liujia150812
        /// </summary>
        /// <param name="iWorkPlanID"></param>
        /// <param name="iRegisteNum"></param>
        /// <param name="OrderId"></param>
        /// <param name="iDetailId"></param>
        /// <returns></returns>
        public static DataSet CancelOrder_Tmkyy(string iWorkPlanID, string iRegisteNum, string OrderId, string iDetailId)
        {
            //if (!PreeRegisterDAL.LHL.AvailableCountDAL.bDataBase)
            //{
            //    return AvailableCountDAL.GetData("CancelOrder.xml");
            //}
            ClinicDb mydb = new ClinicDb();
            try
            {
                SqlCommand cmd = new SqlCommand();//去掉bLock=1的条件和bRegisteFlag=0的条件  lhl20140327
                string sqlStr = @"select iDetailId,bclinicflag,buseflag,bbadflag,bdiagflag 
from opdworkplandetail WITH ( NOLOCK ) where (iWorkPlanID=@iWorkPlanID and iNum=@iRegisteNum and iDetailId=@iDetailId);";
                sqlStr += @"select ipreregister,bimportFlag,bcancelflag,icancelop,dcanceldate,dUpdate,bNowDayFlag
from hmpreregister WITH ( NOLOCK ) where iWorkPlanID=@iWorkPlanID and iprenum=@iRegisteNum and cPreRegisterCode=@OrderId;";
                sqlStr += @"select iWorkPlanID,isnull(npreengageNum,0) npreengageNum,isnull(nMaxPreengageNum,0) nMaxPreengageNum 
from OPDWorkPlan WITH ( NOLOCK ) where iWorkPlanID=@iWorkPlanID;";
                cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@iWorkPlanID", System.Data.SqlDbType.Int));
                cmd.Parameters["@iWorkPlanID"].Value = iWorkPlanID;
                cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@iDetailId", System.Data.SqlDbType.Int));
                cmd.Parameters["@iDetailId"].Value = iDetailId;
                cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@iRegisteNum", System.Data.SqlDbType.Int));
                cmd.Parameters["@iRegisteNum"].Value = iRegisteNum;
                cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@OrderId", System.Data.SqlDbType.VarChar, 20));
                cmd.Parameters["@OrderId"].Value = OrderId;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = sqlStr;
                DataSet ds = mydb.ExecuteSqlCmdFillDs(cmd);
                //AvailableCountDAL.SaveXML("CancelOrder.xml", ds.GetXml());
                return ds;
            }
            catch (Exception err)
            {
                throw err;
            }
            finally
            {
                mydb.Dispose();
            }
        }

        /// <summary>
        /// 根据订单号查询预约信息  liujia151208
        /// </summary>
        /// <param name="ordercode"></param>
        /// <returns></returns>
        public static DataSet CancelOrder_Tmkyy(string ordercode)
        {
            ClinicDb mydb = new ClinicDb();
            try
            {
                string sqlstr = string.Format(@"--检索预约表
                                                select * into #hmpreregister from hmpreregister
                                                where orderCode='{0}'

                                                --查询排班明细表
                                                select a.iDetailId,a.bclinicflag,a.buseflag,a.bbadflag,a.bdiagflag 
                                                from opdworkplandetail a WITH ( NOLOCK ) 
                                                join #hmpreregister b on a.iWorkPlanID=b.iWorkPlanID and a.iNum=b.iprenum

                                                --查询预约表
                                                select ipreregister,bimportFlag,bcancelflag,icancelop,dcanceldate,dUpdate,bNowDayFlag
                                                from #hmpreregister WITH ( NOLOCK ) 

                                                --查询排班主表
                                                select iWorkPlanID,isnull(npreengageNum,0) npreengageNum,isnull(nMaxPreengageNum,0) nMaxPreengageNum 
                                                from OPDWorkPlan WITH ( NOLOCK ) 
                                                where iWorkPlanID in (select iWorkPlanID from #hmpreregister)",ordercode);
                DataSet ds = mydb.ExecuteSelect(sqlstr);
                return ds;
            }
            catch (Exception err)
            {
                throw err;
            }
            finally
            {
                mydb.Dispose();
            }
        }

        public static string SaveData(DataSet ds)
        {
            //if (!PreeRegisterDAL.LHL.AvailableCountDAL.bDataBase)
            //{
            //    return "";
            //}
             ClinicDb mydb = new ClinicDb();
             try
             {
                 mydb.SaveData(ds,true);
                 return "";
             }
             catch(Exception err)
             {
                 return err.ToString();
             }
        }
    }
}
